package com.bytedance.social.service.impl;

import com.bytedance.social.common.JwtUtil;
import com.bytedance.social.common.StatusCode;
import com.bytedance.social.mapper.SocailMapper;
import com.bytedance.social.pojo.User;
import com.bytedance.social.pojo.UserFavorite;
import com.bytedance.social.pojo.Video;
import com.bytedance.social.request.FavoriteActionRequest;
import com.bytedance.social.request.FavoriteListRequest;
import com.bytedance.social.respones.FavoriteActionResponse;
import com.bytedance.social.respones.FavoriteListResponse;
import com.bytedance.social.service.FavoriteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FavoriteServiceImpl implements FavoriteService {

    @Autowired
    private SocailMapper socailMapper;

    /**
     * 赞操作，涉及三张表
     * video和user_favorite和user
     *
     * @param favoriteActionRequest
     * @return
     */
    @Override
    @Transactional
    public FavoriteActionResponse favoriteAction(FavoriteActionRequest favoriteActionRequest) {
        //解析token的载荷
        String token = favoriteActionRequest.getToken();
        //自身userId
        Long userId = JwtUtil.parseToken(token);
        //取出videoId
        Long videoId = favoriteActionRequest.getVideo_id();
        // 1-点赞，2-取消点赞
        Integer actionType = favoriteActionRequest.getAction_type();
        Video video = socailMapper.selectVideoById(videoId);
        FavoriteActionResponse favoriteActionResponse = new FavoriteActionResponse();
        //查询自身的user回来
        User user = socailMapper.selectUserById(userId);
        /**
         * 赞操作，修改video表中的favorite_count
         *
         * 根据被赞videoId查询video表查询出userId去user表修改user表中的total_favorite()
         * 根据自身用户的userId修改user表中的favorite_count(点赞数量)
         */
        //视频作者id，根据被赞videoId查询video表查询出authorId去user表修改user表中的total_favorite
        Long authorId = video.getUser_id();
        User author = socailMapper.selectUserById(authorId);
        //解决用户重新登录，app端重置点赞请求类型bug，先查是否存在点赞关系
        UserFavorite userFavorite = socailMapper.selectUserFavorited(userId, videoId);
        if (actionType == 1 && userFavorite == null) {
            //修改user表中的total_favorite(视频总赞)
            socailMapper.upUserTotalFavor(authorId, author.getTotal_favorited() + 1);
            //根据自身用户的userId修改user表中的favorite_count(点赞数量)
            socailMapper.upUserFavorCount(userId, user.getFavorite_count() + 1);
            //修改video表中的favorite_count
            socailMapper.upVideoFavorite(videoId, video.getFavorite_count() + 1);
            socailMapper.insertUserFavorite(userId, videoId);
            favoriteActionResponse.setStatus_code(StatusCode.STATUS_OK);
            favoriteActionResponse.setStatus_msg("点赞成功！");
            return favoriteActionResponse;
        } else if (actionType == 2) {
            //取消点赞
            //修改user表中的total_favorite(视频总赞)
            socailMapper.upUserTotalFavor(authorId, author.getTotal_favorited() - 1);
            //根据自身用户的userId修改user表中的favorite_count(点赞数量)
            socailMapper.upUserFavorCount(userId, user.getFavorite_count() - 1);
            //修改video表中的favorite_count
            socailMapper.upVideoFavorite(videoId, video.getFavorite_count() - 1);
            //删除点赞关系表中的关系
            socailMapper.deleteUserFavorite(userId, videoId);
            favoriteActionResponse.setStatus_code(StatusCode.STATUS_OK);
            favoriteActionResponse.setStatus_msg("取消点赞成功！");
            return favoriteActionResponse;
        } else {
            favoriteActionResponse.setStatus_msg("你已经点赞过了嗷,请重新下拉刷新查看爱心！");
            favoriteActionResponse.setStatus_code(StatusCode.STATUS_FAIL);
        }

        return favoriteActionResponse;
    }

    /**
     * 获取登录用户的喜欢列表
     *
     * @param favoriteListRequest
     * @return
     */
    @Override
    public FavoriteListResponse getUserFavorite(FavoriteListRequest favoriteListRequest) {
        //获取用户的喜欢列表，根据用户的id查询user_favorite表获取用户的喜欢的videos_id
        Long userId = favoriteListRequest.getUser_id();
        //获取用户喜欢的视频id，再根据视频id去查询video表,获取出作者userId，再根据userId去查询user表
        List<UserFavorite> userFavoriteList = socailMapper.selectUserFavoriteById(userId);
        //对用用户的喜欢video_id遍历
        ArrayList<Video> videos = new ArrayList<>();
        for (UserFavorite userFavorite : userFavoriteList) {
            Long videoId = userFavorite.getVideo_id();
            //根据视频id去查询video表
            Video video = socailMapper.selectVideoById(videoId);
            //获取这个video的author_id
            Long user_id = video.getUser_id();
            //根据userId去查询user表
            User user = socailMapper.selectUserById(user_id);
            /**
             * 对is_follow属性处理
             */
            UserFavorite userFavorite1 = socailMapper.selectUserFavorited(userId, user_id);
            if (userFavorite1 == null) {
                //不存在关注关系
                user.setIs_follow(false);
            } else {
                user.setIs_follow(true);
            }
            //将user设置进video
            video.setAuthor(user);
            videos.add(video);
        }
        FavoriteListResponse favoriteListResponse = new FavoriteListResponse();
        favoriteListResponse.setVideo_list(videos);
        favoriteListResponse.setStatus_code(StatusCode.STATUS_OK);
        favoriteListResponse.setStatus_msg("获取用户喜欢列表成功！");
        return favoriteListResponse;
    }
}
